package com.bgq365.study.springboot2.boot02web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RequestController {

    @GetMapping("/goto")
    public String gotoPage(HttpServletRequest request) {
        request.setAttribute("code", 200);
        request.setAttribute("msg", "成功了");
        return "forward:success";
    }

    @ResponseBody
    @GetMapping("/success")
    public Map<String, Object> success(@RequestAttribute(required = false) Integer code,
                                       @RequestAttribute(required = false) String msg,
                                       HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("msg", msg);
        map.put(("params"), "" + request.getAttribute("model1") + "/" +
                request.getAttribute("map1") + "/" + request.getAttribute("request1"));
        map.put("msg2", request.getAttribute("msg"));
        return map;
    }

    @GetMapping("/params")
    public String testParam(Map<String, Object> map, Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("model1", "哈哈");
        map.put("map1", "mmmap");
        request.setAttribute("request1", "o");

        Cookie cookie = new Cookie("c1", "v1");
        cookie.setDomain("localhost");
        response.addCookie(cookie);
        return "forward:success";
    }
}
